package com.chenyue.cm.match.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.match.domain.ViewMatchItem;
import com.chenyue.cm.match.domain.ViewMatchItemExample;
import com.chenyue.cm.match.service.ViewMatchItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenyue-cm
 * @date 2021-08-03
 */
@RestController
@RequestMapping("/viewMatchItem")
public class ViewMatchItemController {

    private final ViewMatchItemService viewMatchItemService;

    public ViewMatchItemController(ViewMatchItemService viewMatchItemService) {
        this.viewMatchItemService = viewMatchItemService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        Map<String, Object> pageSearch = pageInfo.getPageSearch();
        if (pageSearch == null) {
            pageSearch = new HashMap<>();
        }
        LambdaQueryWrapper<ViewMatchItem> wrapper = new QueryWrapper<ViewMatchItem>().lambda();
        //地区编码
        if (null != pageSearch.get("SEARCH_EQ_area")) {
            wrapper.eq(ViewMatchItem::getProvinceCode, String.valueOf(pageSearch.get("SEARCH_EQ_area")))
                    .or()
                    .eq(ViewMatchItem::getCityCode, String.valueOf(pageSearch.get("SEARCH_EQ_area")))
                    .or()
                    .eq(ViewMatchItem::getAreaCode, String.valueOf(pageSearch.get("SEARCH_EQ_area")));
        }
        //赛事名称
        if (null != pageSearch.get("SEARCH_LIKE_name")) {
            wrapper.like(ViewMatchItem::getName,String.valueOf(pageSearch.get("SEARCH_LIKE_name")));
        }
        wrapper.eq(ViewMatchItem::getState, 1);
        wrapper.orderByDesc(ViewMatchItem::getCt);
        List<ViewMatchItem> list = viewMatchItemService.list(wrapper);
        if(list != null && list.size()>0){
            for (ViewMatchItem viewMatchItem : list) {
                if("全国".equals(viewMatchItem.getProvince())){
                    viewMatchItem.setProvinceCode(1);
                }
                if("全省".equals(viewMatchItem.getCity())){
                    viewMatchItem.setCityCode(2);
                }
                if("全市".equals(viewMatchItem.getArea())){
                    viewMatchItem.setAreaCode(3);
                }
            }
        }
        //封装返回结果
        PageInfo<ViewMatchItem> result = new PageInfo<ViewMatchItem>(list);
        return Success.success(result);
    }

    @RequestMapping("/getCheckList")
    public Success getCheckList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        Map<String, Object> pageSearch = pageInfo.getPageSearch();
        LambdaQueryWrapper<ViewMatchItem> wrapper = new LambdaQueryWrapper<>();
        if (null != pageSearch) {
            //赛事名称
            if (null != pageSearch.get("SEARCH_LIKE_name")) {
                wrapper.like(ViewMatchItem::getName, String.valueOf(pageSearch.get("SEARCH_LIKE_name")));
            }
        }
        wrapper.eq(ViewMatchItem::getState, 0);
        wrapper.orderByDesc(ViewMatchItem::getCt);
        List<ViewMatchItem> list = viewMatchItemService.list(wrapper);
        if(list != null && list.size()>0){
            for (ViewMatchItem viewMatchItem : list) {
                if("全国".equals(viewMatchItem.getProvince())){
                    viewMatchItem.setProvinceCode(1);
                }
                if("全省".equals(viewMatchItem.getCity())){
                    viewMatchItem.setCityCode(2);
                }
                if("全市".equals(viewMatchItem.getArea())){
                    viewMatchItem.setAreaCode(3);
                }
            }
        }
        //封装返回结果
        PageInfo<ViewMatchItem> result = new PageInfo<ViewMatchItem>(list);
        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(ViewMatchItem vo) {
        boolean success = viewMatchItemService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(ViewMatchItem vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = viewMatchItemService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        ViewMatchItemExample aExample = new ViewMatchItemExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = viewMatchItemService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = viewMatchItemService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
    }

}
